package unit02;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author wangzhuangzhuang
 * @DESC: 谓词使用
 * @date 2022-05-08 22:35
 */
public class PredicateDemo {

	// 查找具有给定长度的字符串
	public void getNamesOfLength(List<String> names, int targetLength) {
		Predicate<String> namesIsTargetLength = x -> targetLength == x.length();
		String filteredNames = names.stream()
				.filter(namesIsTargetLength)
				.collect(Collectors.joining(","));
		System.out.println("filteredNames: " + filteredNames);
	}

	// 查找以指定字符串开头的字符串
	public void getNamesOfStartWithAssignedHeader(List<String> names, String targetStr) {
		Predicate<String> namesHeaderIsTargetStr = x -> x.startsWith(targetStr);
		String filteredNames = names.stream()
				.filter(namesHeaderIsTargetStr)
				.collect(Collectors.joining(","));
		System.out.println("filteredNames: " + filteredNames);
	}

	public static void main(String[] args) {
//		List<String> names = new ArrayList<>(Arrays.asList("wangzz", "zzzzz", "wangyt", "Amy"));
//		getNamesSatisfyingCondition(names, PredicateConditions.LENGTH_IS_SIX);
//		System.out.println("======================================================");
//		getNamesSatisfyingCondition(names, PredicateConditions.STARTS_WITH_A);
//		filteredNames: wangzz,wangyt
//		======================================================
//		filteredNames: Amy
		boolean b = Stream.of("a", "b")
				.anyMatch("c"::equals);


	}
	// 查找满足任意谓词的字符串
	public static void getNamesSatisfyingCondition(List<String> names, Predicate<String> condition) {
		String filteredNames = names.stream()
				.filter(condition)
				.collect(Collectors.joining(","));
		System.out.println("filteredNames: " + filteredNames);
	}
}


// 常用的谓词收集到一个类中
class PredicateConditions {
	public static final Predicate<String> LENGTH_IS_SIX = s -> 6 == s.length();
	public static final Predicate<String> STARTS_WITH_A = s -> s.startsWith("A");

	// ...
}